#!/bin/bash
#
# AnalogueRepeater startup script
# Hans-J. Barthen (DL5DI) - dl5di@darc.de
#
# chkconfig: 345 99 01
# description: Starts the G4KLX AnalogueRepeater
# config: /etc/sysconfig/analoguerepeater
#


if [ $UID -ne 0 ]; then
	echo
	     echo "ERROR:  This script must be run as the root user!"
	     echo "        Please use 'su' or log in as root and try again."
	     echo
	     exit 1
fi

me=analoguerepeater

# Source function library.
if [ -f /etc/init.d/functions ] ; then
    . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
    . /etc/rc.d/init.d/functions
else
    exit 1
fi

# Source networking configuration.
[ -r /etc/sysconfig/network ] && . /etc/sysconfig/network

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 1

# Source the configuration
[ -f /etc/sysconfig/$me ] && . /etc/sysconfig/$me

RETVAL=0

dostart() {

    echo -n "Starting $me:"
    daemon --pidfile /var/run/$me.pid $DAEMON_PATH $DAEMON_OPTIONS &
    RETVAL=$?
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$me
    pidofproc $DAEMON_PATH > /var/run/$me.pid
    echo
    # create a watchdog entry to the crontab
    echo "*/10 * * * * root ps aux | grep $DAEMON_PATH | grep -vq grep || /etc/init.d/$me start" > /etc/cron.d/$me
    return $RETVAL
}

dostop() {
    
    echo -n "Stopping $me "
    killproc $DAEMON_PATH
    RETVAL=$?
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$me
    echo
    # remove watchdog entry from crontab
    rm -f /etc/cron.d/$me
    return $RETVAL
}

dorestart() {
    dostop
    sleep 2
    dostart
}	

# See how we were called.
case "$1" in
    start)
	dostart
	;;
    stop)
	dostop
	;;
    restart)
	dorestart
	;;
    status)
	status $DAEMON_PATH
	;;
    *)
	echo -e $"\nUsage: \n\t$0 {option}"
	echo -e "\n\tOptions:\n"
	echo -e "\trestart       - restarts $me\n"
	echo -e "\tstart         - starts $me\n"
	echo -e "\tstatus        - display status\n"
	echo -e "\tstop          - stops $me\n"
	echo -e "\n"
	exit 1
esac

exit $RETVAL
